import util from '../../utils/util.js';
var WXGrid = require('../../utils/wxgrid.js');
var wxgrid = new WXGrid;
var classifies;
var delUserId;

function getPartnerByPlanId(obj, planId) {
  wx.showToast({
    title: '加载中',
    icon: 'loading',
    mask: true
  })

  wx.request({
    url: getApp().globalData.url + '/PLanAndTask/getPartnerByPlanId',
    data: util.json2Form({
      "planId": planId,
    }),
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      wx.hideToast();
      /**下拉出来的东西 */
      wx.stopPullDownRefresh();
      var classifiesTemp;

      classifies = new Array();
      if (res.data.length<=0){
        /**自己 */
        var img = wx.getStorageSync("avatarUrl");
        classifies = [{
          item_delDisplay: "none",
          user_id: wx.getStorageSync("userId"),
          name: wx.getStorageSync("nickName"),
          img
        }]
      }

      for (var i = 0; i < res.data.length; i++) {
        if (res.data[0].create_user_id != wx.getStorageSync("userId")) {
          obj.setData({
            delDisplay: "none"
          })
        }
        console.log("getPartnerByPlanId", res);
        // console.log("userId", wx.getStorageSync("userId"));
        // console.log("datauserId", res.data[i].user_id);
        if (res.data.create_user_id == wx.getStorageSync("userId")) {
          continue;
        }
        var img = res.data[i].avatar_url;
        classifiesTemp = {
          item_delDisplay: obj.data.delDisplay,
          user_id: res.data[i].user_id,
          name: res.data[i].user_name,
          img
        }
        classifies.push(classifiesTemp);
      }

      var col = Math.ceil(classifies.length / 4);
      wxgrid.init(col, 4);

      wxgrid.data.add("classifies", classifies);
      obj.setData({
        wxgrid
      })
    }
  })
}

function quitPartake(obj, planId, userId) {
  obj.setData({
    hidden: true
  })

  wx.showToast({
    title: '加载中',
    icon: 'loading',
    mask: true
  })

  wx.request({
    url: getApp().globalData.url + '/PLanAndTask/quitPartake',
    data: util.json2Form({
      "planId": planId,
      "user_id": userId
    }),
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      console.log("quitPartake", res.data.code);

      if (res.data.code <= 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        wx.hideToast();
        wx.navigateBack({})
      }
    },
    fail: function(e) {
      console.log(e);
    }
  })
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: {
      width: wx.getSystemInfoSync().windowWidth / 4
    },
    wxgrid,
    planName: "",
    planId: "",
    hidden: true,
    modelText: "确定要退出团队吗",
    delDisplay: ""
  },
  delParnter: function(e) {
    delUserId = e.target.id;
    this.setData({
      hidden: false,
      modelText: "确定要踢出该成员吗？"
    })
  },
  cancel: function(e) {
    this.setData({
      hidden: true
    })
  },
  confirm: function(e) {
    quitPartake(this, this.data.planId, delUserId);
  },
  quitPartake: function(e) {
    delUserId = wx.getStorageSync("userId");
    this.setData({
      hidden: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    /**自己 */
    var img = wx.getStorageSync("avatarUrl");
    classifies = [{
      item_delDisplay: "none",
      name: wx.getStorageSync("nickName"),
      img
    }]
    var col = Math.ceil(classifies.length / 4);
    wxgrid.init(col, 4);
    wxgrid.data.add("classifies", classifies);

    this.setData({
      wxgrid,
      planName: options.planName,
      planId: options.planId
    })

    getPartnerByPlanId(this, this.data.planId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    getPartnerByPlanId(this, this.data.planId);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    var obj = this;
    if (e.from === 'button') {
      // 来自页面内转发按钮
      console.log("userId", wx.getStorageSync("userId"))
    }
    // 
    return {
      title: '一起来完成我的计划《' + obj.data.planName + '》',
      // imageUrl: "../images/share.png",
      path: '/pages/sharemessage/sharemessage?userId=' + wx.getStorageSync("userId") +
        "&avatarUrl=" + wx.getStorageSync("avatarUrl") +
        "&nickName=" + wx.getStorageSync("nickName") +
        "&planName=" + obj.data.planName +
        "&planId=" + obj.data.planId
    }
  }
})